package de.hdu.pms.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import de.hdu.pms.model.Basis;


public class BasisDao extends HibernateDaoSupport{
	
	public static Logger Log = Logger.getLogger(BasisDao.class);
	public static void setLog(Logger log){
		Log = log;
	}
	/**
	 * returns all entries from the table Basis with a special id
	 * @return a list of Basis
	 * @see Basis
	 */
	
	public Basis findById(int basis_id){
		HibernateTemplate template = getHibernateTemplate();
		return (Basis) template.get(Basis.class, basis_id);
	}
	
	/**
	 * Lists all entries from the table Basis
	 * @returns als list of Basis
	 * @see Basis
	 * @author BBV
	 */
	
	public List<Basis> findAll(){
		HibernateTemplate template = getHibernateTemplate();
		List<Basis> results = template.loadAll(Basis.class);
		return results;
	}
	
	/**
	 * Saves a entry for the table Basis
	 * @return basis
	 * @param basis
	 * @author BBV
	 */
	
	public Basis save(Basis basis){
		HibernateTemplate template = getHibernateTemplate();
		template.saveOrUpdate(basis);
		return basis;
	}
	
	/**
	 * deletes a entry in the table basis
	 * @param basis
	 * @author BBV
	 */
	public void delete(Basis basis){
		HibernateTemplate template = getHibernateTemplate();
		template.delete(basis);
	}
	
	/** Counts the entries of table basis
	 * @author Boris
	 * @return count
	 */
	
	public int count(){
		try{
			int count = ((Long)getSession().createQuery("select count(*) from Basis").uniqueResult()).intValue();
	        return count;
		}
		catch (Exception e) {
			Log.info("Java Stinkt: Message: " + e.getMessage());
			return (Integer) null;
		}
        
        
 
	}
	
	/**
	 * Lists all bases by Bezeichnung
	 * @author BBV
	 * @param bez
	 * @return results
	 */
	
	@SuppressWarnings("unchecked")
	public List<Basis> findByBez(String bez){
		HibernateTemplate template = getHibernateTemplate();
		List<Basis> results = template.find("from Basis b where b.basis_bez == " + bez);
		return results;
	}
}
